﻿#include "myharmbar.h"

#define TextColor QColor(255,255,255)//白色
#define LColor QColor(0,255,255)//青色
#define BlackColor QColor(0,0,0)
#define Plot_NoColor QColor(0,0,0,0)

#define Plot_DotColor QColor(236,110,0)
#define Plot_LineColor QColor(180,252,253)

#define TextWidth 2
#define LineWidth 1

MyHarmBar::MyHarmBar(QWidget *parent) : QCustomPlot(parent)
{
    init_plotSlot(50);
}

//初始化图表
void MyHarmBar::init_plotSlot(int harmNum)
{
    //设置可拖拽、可放大缩小
    //this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | QCP::iSelectLegend | QCP::iSelectPlottables);
    //设置四边都有坐标
    this->axisRect()->setupFullAxesBox(true);

    //设置坐标颜色/坐标名称颜色
    this->yAxis->setLabelColor(TextColor);
    this->xAxis->setLabelColor(TextColor);

    this->xAxis->setTickLabelColor(TextColor);
    this->yAxis->setTickLabelColor(TextColor);

    this->xAxis->setBasePen(QPen(TextColor, TextWidth));
    this->yAxis->setBasePen(QPen(TextColor, TextWidth));

    this->xAxis->setTickPen(QPen(TextColor, TextWidth));
    this->yAxis->setTickPen(QPen(TextColor, TextWidth));

    this->xAxis->setSubTickPen(QPen(TextColor, TextWidth));
    this->yAxis->setSubTickPen(QPen(TextColor, TextWidth));

    this->yAxis2->setLabelColor(TextColor);
    this->xAxis2->setLabelColor(TextColor);

    this->xAxis2->setTickLabelColor(TextColor);
    this->yAxis2->setTickLabelColor(TextColor);

    this->xAxis2->setBasePen(QPen(TextColor, TextWidth));
    this->yAxis2->setBasePen(QPen(TextColor, TextWidth));

    this->xAxis2->setTickPen(QPen(TextColor, TextWidth));
    this->yAxis2->setTickPen(QPen(TextColor, TextWidth));

    this->xAxis2->setSubTickPen(QPen(TextColor, TextWidth));
    this->yAxis2->setSubTickPen(QPen(TextColor, TextWidth));

    //不显示X轴和Y轴小刻度
    //this->xAxis->setTickLabels(false);
    //this->xAxis->setTicks(false);
    //this->yAxis->setSubTicks(false);

    //不显示X轴上边和Y轴右边刻度
    this->xAxis2->setTickLabels(false);
    this->xAxis2->setTicks(false);

    this->yAxis2->setTickLabels(false);
    this->yAxis2->setTicks(false);

    //设置画布背景色
    QLinearGradient plotGradient;
    plotGradient.setStart(0, 0);
    plotGradient.setFinalStop(0, 350);
    plotGradient.setColorAt(0, QColor(80, 80, 80));
    plotGradient.setColorAt(1, QColor(50, 50, 50));
    this->setBackground(plotGradient);

    //设置坐标背景色
    QLinearGradient axisRectGradient;
    axisRectGradient.setStart(0, 0);
    axisRectGradient.setFinalStop(0, 350);
    axisRectGradient.setColorAt(0, QColor(80, 80, 80));
    axisRectGradient.setColorAt(1, QColor(30, 30, 30));
    this->axisRect()->setBackground(axisRectGradient);

    //设置图例提示位置及背景色
    // ui->pCustomPlot_V->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);
    // ui->pCustomPlot_V->axisRect()->insetLayout()->setInsetAlignment(0, Qt::Alignment);
    // ui->Custom_V->legend->setBrush(QColor(255, 255, 255, 200));
    this->axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop | Qt::AlignRight);
    this->legend->setVisible(true);
    this->legend->setBorderPen(QPen(QColor(Qt::transparent)));
    this->legend->setBrush(QColor(Qt::transparent));

    QFont legendFont = font();
    legendFont.setPointSize(10);
    this->legend->setFont(legendFont);
    this->legend->setTextColor(QColor(Qt::white));
    this->legend->setSelectedFont(legendFont);
    this->legend->setSelectableParts(QCPLegend::spItems);
    this->replot();

    //设置可拖拽、横坐标可放大缩小
    //this->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);

    //设置点击图例颜色变化
    //this->axisRect(0)->setRangeZoomFactor(2, 1);

    //设置横坐标
    //this->yAxis->grid()->deleteLater();
    //this->xAxis->grid()->deleteLater();

    // 设置静态曲线的横坐标范围及自适应横坐标
    this->xAxis->setRange(0, harmNum+1);
    this->yAxis->setRange(0, 100);

    //设置X/Y坐标名称
    this->yAxis->setLabel(tr(""));
    this->xAxis->setLabel(tr(""));
    //点击图例操作
    //connect(this, SIGNAL(legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*,QMouseEvent*)), this,SLOT(legendDoubleClick_Press(QCPLegend*,QCPAbstractLegendItem*)));
    this->replot();

}

//************************************
// 函数名称:   dealDrawBarChart()
// 函数说明： 绘制柱形图
// 返 回 值: void
//************************************
void MyHarmBar::dealDrawBarChart(QVector<double> xBarVector, QVector<double> yBarVector)
{
    int plottableCount = this->plottableCount();
     if (plottableCount != 0)
          this->clearPlottables();

    if(xBarVector.length()==0 || yBarVector.length()==0)
    {
        this->replot();
        return;
    }

    QVector<double> x(xBarVector);
    QVector<double> y(yBarVector);
    qSort(x);
    qSort(y);
    this->xAxis->setRange(0, xBarVector.length()+1);
    this->yAxis->setRange(0, 100);

    this->legend->setVisible(false);
    QLinearGradient gradient(0, 0, 0, 400);
    gradient.setColorAt(0, QColor(90, 90, 90));
    gradient.setColorAt(0.38, QColor(100, 100, 100));
    gradient.setColorAt(1, QColor(70, 70, 70));
    this->setBackground(QBrush(gradient));

    QCPBars *rectBar = new QCPBars(this->xAxis, this->yAxis);
    rectBar->setAntialiased(false);
    rectBar->setStackingGap(1);
    rectBar->setPen(QPen(QColor(0, 168, 140).lighter(130)));
    rectBar->setBrush(QColor(0, 168, 140));
    rectBar->setData(xBarVector, yBarVector);
    this->replot();
}

void MyHarmBar::setData(double* harmData,int dataLen)
{
    QVector<double> xBarVector;
    QVector<double> yBarVector;
    double jb=*(harmData+1);
    double xb=0;
    //0次、1次谐波为直流和基波，这里写死
    xBarVector<<0<<1;
    yBarVector<<0<<100;

    for(int i=2;i<dataLen;i++)
    {
        xBarVector<<i;
        xb=*(harmData+i);
        if(xb>jb)
            xb=0;
        yBarVector<<xb / jb * 100.0;
    }

    dealDrawBarChart(xBarVector,yBarVector);
}
